import 'dart:io';

import 'package:deepseek_desktop/components/Avatar.dart';
import 'package:deepseek_desktop/components/ChatHistoryController.dart';
import 'package:deepseek_desktop/entity/ChatSimpleItem.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import "package:intl/intl.dart";

class ChatListItem extends StatelessWidget {
  final ChatSimpleItem item;

  ChatListItem({super.key, required this.item});

  ChatHistoryController controller = Get.put(ChatHistoryController());

  @override
  Widget build(BuildContext context) {
    Color textColor = const Color.fromARGB(255, 176, 155, 206);
    Color selectColor = const Color.fromARGB(255, 77, 107, 254);

    return Listener(
      onPointerDown: (details) {
        if (details.kind == PointerDeviceKind.mouse &&
            details.buttons == kSecondaryMouseButton) {
          _showPopupMenu(context, details.position);
        }
      },
      child: Obx(() => Container(
            decoration: BoxDecoration(
              border: controller.selectedChatId.value == item.id
                  ? const Border(
                      left: BorderSide(color: Colors.white, width: 3))
                  : const Border(
                      left: BorderSide(color: Colors.transparent, width: 3)),
              color: controller.selectedChatId.value == item.id
                  ? selectColor.withOpacity(0.3)
                  : Colors.transparent,
            ),
            child: MaterialButton(
              onPressed: () {
                controller.selectChat(item.id);
              },
              child: Container(
                padding: const EdgeInsets.all(10),
                child: Row(
                  children: [
                    if (item.avatar.isNotEmpty)
                      CircleAvatar(
                        backgroundImage: Image.asset(item.avatar).image,
                      )
                    else
                      // CircleAvatar(
                      //   child: Avatar(
                      //     filePath: 'assets/chatgpt_c.ico',
                      //     size: 40,
                      //     role: 'assistant',
                      //   ),
                      // ),
                      // Avatar(
                      //   filePath: 'assets/chatgpt_c.png',
                      //   size: 40,
                      //   role: 'assistant',
                      // ),
                      SizedBox(
                        width: 40,
                        height: 40,
                        child: Image.asset(
                          'assets/deepseek.png',
                          fit: BoxFit.cover,
                        ),
                      ),
                    const SizedBox(
                      width: 6,
                    ),
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            item.name,
                            overflow: TextOverflow.ellipsis,
                            style: const TextStyle(
                                color: Colors.white,
                                fontWeight: FontWeight.w500,
                                fontSize: 13),
                          ),
                          Text(
                            item.lastMessage.isEmpty
                                ? '思考更深，推理更强'
                                : item.lastMessage,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(color: textColor, fontSize: 12),
                          ),
                        ],
                      ),
                    ),
                    const SizedBox(
                      width: 6,
                    ),
                    Text(getSimpleLastMessageTime(item.lastMessageTime),
                        style: TextStyle(color: textColor, fontSize: 10)),
                  ],
                ),
              ),
            ),
          )),
    );
  }

  void _showPopupMenu(BuildContext context, Offset position) {
    final RenderBox overlay =
        Overlay.of(context)!.context.findRenderObject() as RenderBox;

    showMenu(
      context: context,
      position: RelativeRect.fromRect(
        position & Size(40, 40),
        Offset.zero & overlay.size,
      ),
      items: <PopupMenuEntry>[
        const PopupMenuItem(
          value: 'delete',
          child: Row(
            children: [
              Icon(
                Icons.delete,
              ),
              SizedBox(
                width: 6,
              ),
              Text(
                'Delete',
                style: TextStyle(fontSize: 12),
              ),
            ],
          ),
        ),
      ],
      elevation: 8.0,
    ).then((value) {
      if (value != null) {
        if (value == 'delete') {
          // 删除
          controller.removeChat(item);
        }
      }
    });
  }

  String getSimpleLastMessageTime(String time) {
    if (time.isEmpty) {
      return '';
    }
    if (time.length < 19) {
      time += ':00';
    }
    final inputFormat = DateFormat('yyyy-MM-dd HH:mm:ss');
    final outputFormat = DateFormat('MM/dd');
    final timeFormat = DateFormat('HH:mm');
    final now = DateTime.now();
    final today = DateTime(now.year, now.month, now.day);
    final yesterday = today.subtract(Duration(days: 1));

    final inputDate = inputFormat.parse(time);
    final inputDay = DateTime(inputDate.year, inputDate.month, inputDate.day);

    if (inputDate.isAfter(now.subtract(Duration(minutes: 1)))) {
      return 'Now';
    } else if (inputDate.isAfter(today)) {
      return timeFormat.format(inputDate);
    } else if (inputDay == today) {
      return 'Today';
    } else if (inputDay == yesterday) {
      return 'Yesterday';
    } else {
      return outputFormat.format(inputDate);
    }
  }
}
